/*
 * Copyright (C) 2001-2005 Pleasant nightmare studio
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package org.pleasantnightmare.acake.errorhandling;

import java.util.Arrays;

/**
 * @author deus
 * @version 1.0
 * @since Nov 17, 2009 11:01:02 AM
 */
public class CheckError {
    public static final int DEFAULT_INFO_LEVEL = 100;
    public static final int DEFAULT_WARN_LEVEL = 200;
    public static final int DEFAULT_ERROR_LEVEL = 300;

    private int errorCode;
    private String messagePattern;
    private int level;
    private Object[] messageParams;

    public CheckError(int errorCode) {
        this(errorCode, "");
    }

    public CheckError(int errorCode, String messagePattern) {
        this(errorCode, messagePattern, DEFAULT_ERROR_LEVEL);
    }

    public CheckError(int errorCode, String messagePattern, int level) {
        this.errorCode = errorCode;
        this.messagePattern = messagePattern;
        this.level = level;
    }

    public String getMessage() {
        return String.format(messagePattern, messageParams);
    }

    public String getMessagePattern() {
        return messagePattern;
    }

    public int getLevel() {
        return level;
    }

    public int getErrorCode() {
        return errorCode;
    }

    public void setMessageParams(Object[] messageParams) {
        this.messageParams = messageParams;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        CheckError that = (CheckError) o;

        if (errorCode != that.errorCode) return false;
        if (level != that.level) return false;
        // Probably incorrect - comparing Object[] arrays with Arrays.equals
        if (!Arrays.equals(messageParams, that.messageParams)) return false;
        if (!messagePattern.equals(that.messagePattern)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = errorCode;
        result = 31 * result + messagePattern.hashCode();
        result = 31 * result + level;
        result = 31 * result + Arrays.hashCode(messageParams);
        return result;
    }

    @Override
    public String toString() {
        return String.format("CheckError{errorCode=%d, messagePattern='%s\', level=%d, messageParams=%s}", errorCode, messagePattern, level, (messageParams == null ? null : Arrays.asList(messageParams)));
    }
}
